package com.shg.bean;

import java.util.List;

public class PageView<E> {
	// 分页数据
	private List<E> records;
	// 页码开始索引和结束索引
	private PageIndex pageindex;
	// 总页数
	private long totalpage = 1;
	// 每页显示记录数（默认每页显示12条）
	private int maxrecord = 12;
	// 当前页(默认是第一页)
	private int currentpage = 1;
	// 总记录数
	private long totalrecord;
	// 页码数量(现实在页面端的最大页码量)
	private int pagecode = 10;
	/**
	 * 构造函数
	 * @param maxrecord 每页显示的记录数
	 * @param currentpage 当前显示页码数
	 */
	public PageView(int maxrecord, int currentpage) {
		this.maxrecord = maxrecord;
		this.currentpage = currentpage;
	}

	// 要获取记录的开始索引
	public int getFirstResult() {
		return (this.currentpage - 1) * this.maxrecord;
	}
	//将分页查询出来的数据存储在（其中包括查询出来的总记录数和每一条记录信息）
	public void setQueryResult(QueryResult<E> qr){
		setTotalrecord(qr.getResultCount());
		setRecords(qr.getResultList());
	}
	
	public List<E> getRecords() {
		return records;
	}

	public void setRecords(List<E> records) {
		this.records = records;
	}

	public PageIndex getPageindex() {
		return pageindex;
	}

	public void setPageindex(PageIndex pageindex) {
		this.pageindex = pageindex;
	}

	public long getTotalpage() {
		return totalpage;
	}

	public void setTotalpage(long totalpage) {
		this.totalpage = totalpage;
		this.pageindex = PageIndex.getPageIndex(pagecode, currentpage, totalpage);
	}


	public int getMaxrecord() {
		return maxrecord;
	}

	public void setMaxrecord(int maxrecord) {
		this.maxrecord = maxrecord;
	}

	public int getCurrentpage() {
		return currentpage;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public long getTotalrecord() {
		return totalrecord;
	}
	/**
	 * 通过总记录数计算出总页数
	 * @param totalrecord
	 */
	public void setTotalrecord(long totalrecord) {
		this.totalrecord = totalrecord;
		setTotalpage(this.totalrecord%this.maxrecord==0? this.totalrecord/this.maxrecord : this.totalrecord/this.maxrecord+1);
	}

	public int getPagecode() {
		return pagecode;
	}

	public void setPagecode(int pagecode) {
		this.pagecode = pagecode;
	}

}
